package com.ChenYaRou.www.controller.servlet;

import com.ChenYaRou.www.controller.*;
import com.ChenYaRou.www.po.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.ChenYaRou.www.constant.Constant.*;
import static com.ChenYaRou.www.constant.HomePage.*;
import static com.ChenYaRou.www.constant.ShopConstant.*;

/**
 * @ClassName IntoShopServlet
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/6 0:17
 * @Version 1.0
 **/
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        JSONObject json = JSON.parseObject(req.getParameter("json"));
        String mode = json.getString("mode");
        if(GET_THE_SHOP_AND_FOOD.equals(mode)){
            out.write(new ShopServletController().getTheShopAndFood(json.getInteger("shopId")));
        }else if(GET_MESSAGE.equals(mode)){
            out.write(new MessageServletController().getShopAllMessage(json.getInteger("shopId")));
        }else if(USER_HAS_THE_SHOP_TICKETS.equals(mode)){
            out.write(new GetTicketServletController().userHasTheShopTicket(new Ticket(
                    json.getInteger("userId"), json.getInteger("shopId"), UN_USE,null,null)));
        }else if(TAKE_ORDERS_NO_TICKET.equals(mode) || TAKE_ORDERS_HAS_TICKET.equals(mode)){
            Orders orders=new Orders(NO_ACCEPT, json.getInteger("userId"), json.getInteger("shopId"),
                    json.getString("content"), json.getFloat("cost"),
                    LocalDateTime.now(), LocalDateTime.now());
            List<Orders> ordersList=new ArrayList<>();
            ordersList.add(orders);
            List<Ticket> ticketList=new ArrayList<>();
            if(TAKE_ORDERS_HAS_TICKET.equals(mode)){
                Ticket ticketOld=new Ticket();
                ticketOld.setId(json.getInteger("ticketId"));
                ticketList.add(ticketOld);
            }
            out.write(new TakeOrdersServletController().takeOrders(ordersList, json.getString("userName"), ticketList));
        }else if(COLLECTION.equals(mode)){
            out.write(new ShopServletController().addCollection(new Collection(json.getInteger("userId"),
                    json.getInteger("shopId"),SHOW)));
        }else if(GET_THE_SHOP_AND_FOOD_BY_OWNER_ID.equals(mode)){
            out.write(new ShopServletController().getTheShopAndFoodByOwnerId(json.getInteger("ownerId")));
        }else if(ORDERS.equals(mode)){
            Orders orders=new Orders();
            orders.setShopId(json.getInteger("id"));
            orders.setState(json.getString("state"));
            out.write(new PersonalHomePageServletController().personalHomePageOrders(orders));
        }else if(INSERT_NEW_FOOD.equals(mode)){
            Food food=new Food(json.getString(NAME), json.getFloat(PRICE), json.getInteger("shopId"));
            out.write(new ShopServletController().insertFood(food));
        }else if(INSERT_TICKET.equals(mode)){
            TicketShop ticketShop=new TicketShop(
                    json.getInteger("shopId"), json.getFloat("discount"), json.getInteger("number"));
            out.write(new GetTicketServletController().shopInsertTicket(ticketShop));
        }else if(UPDATE_TICKET.equals(mode)){
            TicketShop ticketShop=new TicketShop(
                    json.getInteger("shopId"), json.getFloat("discount"), json.getInteger("number"));
            out.write(new GetTicketServletController().shopUpdateTicket(ticketShop));
        }else if(DELETE_MESSAGE.equals(mode)){
            Message message=new Message();
            message.setId(json.getInteger("messageId"));
            System.out.println(message);
            out.write(new MessageServletController().deleteMessage(message));
        }else if(UPDATE_ORDERS.equals(mode)){
            String state = json.getString("state");
            Integer ordersId = json.getInteger("ordersId");
            Integer shopId = json.getInteger("id");
            Float cost = json.getFloat("cost");
            Integer buyerId = json.getInteger("buyerId");

            Orders orders = new Orders();
            orders.setState(state);
            orders.setId(ordersId);
            orders.setShopId(shopId);
            orders.setCost(cost);
            orders.setBuyerId(buyerId);
            orders.setUpdateTime(LocalDateTime.now());

            System.out.println(orders);
            out.write(new ShopServletController().updateOrders(orders));
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
